/* Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. */
import androidx.compose.foundation.*
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.window.WindowDraggableArea
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Alignment.Companion.CenterVertically
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.RectangleShape
import androidx.compose.ui.input.pointer.PointerIcon
import androidx.compose.ui.input.pointer.pointerIcon
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.input.pointer.pointerMoveFilter
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.*

val topBarHeight = 60.dp

@OptIn(ExperimentalDesktopApi::class, androidx.compose.ui.ExperimentalComposeUiApi::class,
    androidx.compose.material.ExperimentalMaterialApi::class, androidx.compose.animation.ExperimentalAnimationApi::class
)
fun main() = application {
    val isOpen = remember { mutableStateOf(true) }
    if (isOpen.value) {
        val windowState = rememberWindowState()
        Window(undecorated = true,
            onCloseRequest={},
            state = windowState
        ){
            window.minimumSize = window.size
            Column {
                //双击全屏或者恢复
                WindowDraggableArea(modifier = Modifier.pointerInput(Unit){
                    detectTapGestures(onDoubleTap = {
                        windowState.placement = if (windowState.placement == WindowPlacement.Fullscreen){
                            WindowPlacement.Floating
                        }else{
                            WindowPlacement.Fullscreen
                        }
                    })
                }) {
                    TopBar(isOpen,windowState)
                }
                app(windowState)
            }
        }
    }
}

@Composable
fun app(windowState: WindowState){
    val musicBarHeight = 75.dp
    Column {
        Row(Modifier.height(windowState.size.height-topBarHeight-musicBarHeight)) {
            Spacer(modifier = Modifier.width(10.dp))
            SelectSidebar(modifier = Modifier.fillMaxHeight())
        }
        Spacer(Modifier.heightIn(15.dp))
        Divider(modifier = Modifier.fillMaxWidth().heightIn(1.dp))
        MusicView(MusicInfoViewModel("p1.png","电脑","V.W.P",240),musicBarHeight)
    }
}
@OptIn(ExperimentalComposeUiApi::class, androidx.compose.foundation.ExperimentalDesktopApi::class)
@Composable
fun SelectSidebar(modifier: Modifier = Modifier){
    val scrollState = rememberScrollState()
    var index = 0
    var mouseEnterState by remember { mutableStateOf(true) }
    var checkIndex by remember { mutableStateOf(-1) }
    val list = listOf("发现音乐","播客","视频","直播","私人FM")
    Row(modifier) {
        Column(modifier = Modifier
            .verticalScroll(scrollState)
            .width(120.dp)
            .pointerMoveFilter(
                onEnter = {
                    mouseEnterState = true
                    false
                },
                onExit = {
                    mouseEnterState = false
                    false
                }
            )
        )
        {
            list.forEach{
                val itemIndex = index++
                val checkedState = remember { mutableStateOf( false)  }
                checkedState.value = itemIndex == checkIndex
                SelectSideItem(it,checkedState){
                    checkIndex = itemIndex
                }
                Spacer(Modifier.height(5.dp))
            }
            Text("我的歌单",style = TextStyle(color = Color.Gray,fontSize = 10.sp))

            Spacer(Modifier.height(10.dp))

            repeat(10){
                Row {
                    val itemIndex = index++
                    val checkedState = remember { mutableStateOf( false)  }
                    checkedState.value = itemIndex == checkIndex
                    Spacer(Modifier.width(5.dp))
                    SelectSideItem("Aimer",checkedState,{Icon(painter = painterResource("musicForm.svg"),contentDescription = null,modifier = Modifier.height(20.dp))}){
                        checkIndex = itemIndex
                    }
                    Spacer(Modifier.height(20.dp))
                }
            }
        }
        if (mouseEnterState) {
            VerticalScrollbar(ScrollbarAdapter(scrollState))
        }
    }
}
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun SelectSideItem(text:String,isChecked:MutableState<Boolean>, icon: @Composable (() -> Unit?)? =null, onclick:()->Unit={}){
    var fontWeight by remember { mutableStateOf(FontWeight.W300) }
    fontWeight = if(isChecked.value){
        FontWeight.W600
    }else{
        FontWeight.W300
    }
    val color = if(isChecked.value) {Color(245,245,246)}else {MaterialTheme.colors.surface}
    Card(
        modifier = Modifier
            .height(40.dp)
            .fillMaxWidth()
            .clickable{
                onclick()
            }
            .pointerIcon(PointerIcon.Hand),
        elevation = 0.dp,
        backgroundColor = color
    ){
        Box(contentAlignment = Alignment.CenterStart,modifier = Modifier.fillMaxHeight().fillMaxWidth()){
            Row {
                if (icon!=null){
                    icon()
                }
                Text(text,style = TextStyle(fontWeight = fontWeight))
            }
        }
    }

}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun TopBar(isOpen:MutableState<Boolean>,windowState: WindowState){
    TopAppBar(
        title = {
            Box(contentAlignment = Alignment.TopStart){
                Icon(painterResource("appLogo.svg"),contentDescription = null)
            }
            Spacer(modifier = Modifier.width(10.dp))
            Text("网易云音乐")
        },
        actions = {
            IconButton(
                modifier = Modifier.pointerIcon(PointerIcon.Hand),
                onClick = {
                    windowState.isMinimized = !windowState.isMinimized
                }
            ) {
                Icon(painterResource("minimize.svg"),null)
            }
            IconButton(
                modifier = Modifier.pointerIcon(PointerIcon.Hand),
                onClick = {
                    windowState.placement = if (windowState.placement == WindowPlacement.Fullscreen){
                        WindowPlacement.Floating
                    }else{
                        WindowPlacement.Fullscreen
                    }
                }
            ) {
                Icon(painterResource("maximize.svg"),null)
            }

            IconButton(
                onClick = {
                    isOpen.value = false
                },
                modifier = Modifier.pointerIcon(PointerIcon.Hand)
            ) {
                Icon(painterResource("close.svg"), null)
            }
        },
        modifier = Modifier.height(topBarHeight)
    )
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun MusicView(musicInfoViewModel: MusicInfoViewModel,height:Dp){
    var isMax by remember { mutableStateOf(false) }
    Row(Modifier.height(height),verticalAlignment = CenterVertically) {
        Surface(shape = RoundedCornerShape(25),modifier = Modifier.size(75.dp).padding(horizontal = 10.dp,vertical = 10.dp).pointerIcon(PointerIcon.Hand)) {
            Image(painter = painterResource(musicInfoViewModel.photoPath),contentDescription = null,
                modifier = Modifier.clickable(){ isMax = true }
            )
        }
         Text("${musicInfoViewModel.name}\n${musicInfoViewModel.author}",modifier = Modifier.height(40.dp).width(160.dp))
     }
    if (isMax){
        Dialog(onCloseRequest = {isMax=false}){
            Image(painter = painterResource(musicInfoViewModel.photoPath),contentDescription = null)
        }
    }
}




